package com.czf.company.controller;

import com.czf.common.entity.CompanyBaseInfo;
import com.czf.common.entity.CreditCompany;
import com.czf.common.util.AjaxResult;
import com.czf.common.util.CommonUtils;
import com.czf.company.service.CompanyBaseInfoSerivceImpl;
import com.czf.company.service.CreditCompanyServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zfChen
 * @create 2022/4/24 17:16
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyBaseInfoSerivceImpl companyBaseInfoSerivce;
    @Autowired
    private CreditCompanyServiceImpl creditCompanyService;

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public AjaxResult register(@RequestBody CreditCompany creditCompany) {
        creditCompany.setUserId(CommonUtils.uuid());
        creditCompany.setRegisterTime(CommonUtils.timeStamp());
        int register = creditCompanyService.register(creditCompany);
        if (register == 1) {
            return AjaxResult.success(register);
        }
        return AjaxResult.error();
    }

    @RequestMapping(value = "login", method = RequestMethod.GET)
    public AjaxResult login(CreditCompany creditCompany) {
        String account = creditCompany.getAccount();
        String password = creditCompany.getPassword();
        Long login = creditCompanyService.login(account, password);
        if (ObjectUtils.isNotEmpty(login)) {
            return AjaxResult.success(login);
        }
        return AjaxResult.error();
    }

    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public AjaxResult select(Long userId) {
        CreditCompany select = creditCompanyService.select(userId);
        if (ObjectUtils.isNotEmpty(select)) {
            return AjaxResult.success(select);
        }
        return AjaxResult.error();
    }

    @RequestMapping(value = "/hold", method = RequestMethod.POST)
    public AjaxResult hold(@RequestBody CompanyBaseInfo companyBaseInfo) {
        CompanyBaseInfo select = companyBaseInfoSerivce.select(companyBaseInfo.getUserId());
        if (ObjectUtils.isNotEmpty(select)) {
            int update = companyBaseInfoSerivce.update(companyBaseInfo);
            if (update == 1) {
                return AjaxResult.success(update);
            }
        } else {
            int insert = companyBaseInfoSerivce.insert(companyBaseInfo);
            if (insert == 1) {
                return AjaxResult.success(insert);
            }
        }
        return AjaxResult.error();
    }

    @RequestMapping(value = "/getCompanyInfo", method = RequestMethod.GET)
    public AjaxResult getCompanyInfo(Long userId) {
        CompanyBaseInfo select = companyBaseInfoSerivce.select(userId);
        if (ObjectUtils.isNotEmpty(select)) {
            return AjaxResult.success(select);
        }
        return AjaxResult.error();
    }
}
